'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 用户相关
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var Code = require('../../../consts/code');
var moduleType = require('../../../consts/moduleType');
var operationType = require('../../../consts/operationType');
var PlayerManager = require('../../../libs/playerManager');
var roleService = require('../../../services/roleService');
var mailService = require('../../../services/mailService');
var roleModel = require('../../../models/roleModel');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 添加钻石
 */
Handler.prototype.addDiamond = function (msg, session, next) {
    var role_id = msg.query.role_id;
    var add_diamond = msg.query.add_diamond;

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['role', 'task', 'daily_task', 'achieve', 'sevenDayActivity', 'accrued'], cb);
            },
            function (cb) {
                roleService.addDiamond(player_manager, moduleType.MDL_ADMIN, operationType.OPR_ADMIN_SEND, add_diamond);
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};

/**
 * 添加金币
 */
Handler.prototype.addGold = function (msg, session, next) {
    var role_id = msg.query.role_id;
    var add_gold = msg.query.add_gold;

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['role', 'accrued'], cb);
            },
            function (cb) {
                roleService.addGold(player_manager, moduleType.MDL_ADMIN, operationType.OPR_ADMIN_SEND, add_gold);
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};

/**
 * 添加粮食
 */
Handler.prototype.addFood = function (msg, session, next) {
    var role_id = msg.query.role_id;
    var add_food = msg.query.add_food;

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['role', 'task', 'daily_task', 'achieve', 'sevenDayActivity', 'accrued'], cb);
            },
            function (cb) {
                roleService.addFoodLimit(player_manager, moduleType.MDL_ADMIN, operationType.OPR_ADMIN_SEND, add_food);
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};

/**
 * 添加玩家邮件
 */
Handler.prototype.addMail = function (msg, session, next) {
    var role_id = msg.query.role_id;
    var title = msg.query.title;
    var content = msg.query.content;
    var attach_list = msg.query.attach_list;
    var email_guid = msg.query.email_guid;

    var attach = JSON.parse(attach_list);
    email_guid = parseInt(email_guid);
    _sendMail(role_id, title, content, attach, email_guid, next);
};

/**
 * 添加玩家邮件
 */
Handler.prototype.addAllMail = function (msg, session, next) {
    var title = msg.query.title;
    var content = msg.query.content;
    var attach_list = msg.query.attach_list;
    var email_guid = msg.query.email_guid;
    email_guid = parseInt(email_guid);
    var attach = JSON.parse(attach_list);
    roleModel.getAllRoleList(function (err, role_list) {
        async.each(role_list, function (role_item, callback) {
            _sendMail(role_item.role_id, title, content, attach, email_guid, function () {
                callback();
            });
        }, function (err) {
            return next(null, {
                code: Code.OK
            });
        });
    });
};

/**
 * 添加邮件
 * @param  {[type]} role_id     [description]
 * @param  {[type]} title       [description]
 * @param  {[type]} content     [description]
 * @param  {[type]} attach_list [description]
 * @return {[type]}             [description]
 */
var _sendMail = function (role_id, title, content, attach, email_guid, next) {
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            //function (cb) {
                //player_manager.refreshData(cb);
            //},
            function (cb) {
                player_manager.getModel(
                    ['mail'],
                    cb
                );
            },
            function (cb) {
                var mail_model = player_manager.mail;
                // 加入后台重复验证
                if (!mail_model.isEmailGuid(email_guid)) {
                    return next(null, {
                        code: Code.FAIL
                    });
                }
                mail_model.addEmailGuid(email_guid);
                mailService.sendMail(mail_model, title, content, attach, moduleType.MDL_ADMIN, operationType.OPR_ADMIN_SEND);
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.MAIL_DOUBLE_ERROR
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};
